﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MQTTnet;
using MQTTnet.Client;
using QModels;
using QServices;
using QServices.Services;
using S7.Net.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace QControlCenter.ViewModel
{
    public partial class AGVCarPageViewModel: QBaseVM
    {
		

		[ObservableProperty]
		private string? carNumber;

		[ObservableProperty]
		private string? carId;

		[ObservableProperty]
		private string? carStatusIcon;

		[ObservableProperty]
		private string? carIcon;
		[ObservableProperty]
		private string? region;
		[ObservableProperty]
		private int first_80;
		[ObservableProperty]
		private int second_80;
		[ObservableProperty]
		private int third_80;
		[ObservableProperty]
		private int first_120;
		[ObservableProperty]
		private int second_120;
		[ObservableProperty]
		private int third_120;
		[ObservableProperty]
		private int first_160;
		[ObservableProperty]
		private int second_160;
		[ObservableProperty]
		private int third_160;
		[ObservableProperty]
		private int first_200;
		[ObservableProperty]
		private int second_200;
		[ObservableProperty]
		private int third_200;
		[ObservableProperty]
		private int hourFirst;
		[ObservableProperty]
		private int minuteFirst;
		[ObservableProperty]
		private int hourSecond;
		[ObservableProperty]
		private int minuteSecond;
		[ObservableProperty]
		private int hourThird;
		[ObservableProperty]
		private int minuteThird;
		[ObservableProperty]
		private string? iP;
		[ObservableProperty]
		private decimal? maxElectricQty;
		[ObservableProperty]
		private decimal? minAllowEleQty;
		[ObservableProperty]
		private decimal electricity;
		[ObservableProperty]
		private decimal electricityWidth;


		private static Dictionary<string, string> AGVCARICON = new Dictionary<string, string>
		{
			{"A","/images/car_a01.svg" },
			{"B","/images/car_b02.svg" },
			{"C","/images/car_c03.svg" },
			{"D","/images/car_d04.svg" },
			{"E","/images/car_e05.svg" },

		};
		public AGVCarPageViewModel()
		{
			
		}



		[RelayCommand]
		public async void SaveAGVCarInfo()
		{
			var car = new AgvCar
			{
				CarNumber = CarNumber,
				MinAllowEleQty = MinAllowEleQty,
				MaxElectricQty = MaxElectricQty,
				IP = IP
			};
			var schedule = new DosingSchedule
			{
				CarId=CarId,
				First_80=First_80,
				Second_80=Second_80,
				Third_80=Third_80,

				First_120 = First_120,
				Second_120 = Second_120,
				Third_120 = Third_120,

				First_160 = First_160,
				Second_160 = Second_160,
				Third_160 = Third_160,

				First_200=First_200,
				Second_200=Second_200,
				Third_200=Third_200,
				
				HourFirst=HourFirst,
				HourSecond=HourSecond,
				HourThird=HourThird,

				MinuteFirst=MinuteFirst,
				MinuteSecond=MinuteSecond,
				MinuteThird=MinuteThird,

			};

			AppRuntime.GetService().GetService<AgvCarService>().UpdateCarInfoAndSchedule(car, schedule);

			Growl.SuccessGlobal("保存成功!");
			AppRuntime.Logger_Info("保存小车版面信息+"+JsonSerializer.Serialize(schedule));
			#region  通知更新电量
			try
			{
				if (!LsdViewModel.m_mqttclient.IsConnected) await LsdViewModel.m_mqttclient.ReconnectAsync();
				var message = new MqttApplicationMessage
				{
					PayloadSegment = Encoding.UTF8.GetBytes("OK"),
					Topic = "UpdateCarStatus",
					QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce,
					
				};
				await LsdViewModel.m_mqttclient.PublishAsync(message);
				//await LsdViewModel.m_mqttclient.PublishAsync(message);
				
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error("mqtt连接异常");
			}
			
			#endregion
			
		}

		public void InitData()
		{
			if (string.IsNullOrWhiteSpace(Region)) return;
			var car=AppRuntime.GetService().GetService<AgvCarService>().GetAgvCarByRegion(Region);
			if (car == null) return;
			var schedule = AppRuntime.GetService().GetService<DosingScheduleService>().GetDosingScheduleByCarId(car.ID);
			if (schedule == null) return;
			CarIcon = AGVCARICON[Region];
			this.UpdateFrom(car);
			this.UpdateFrom(schedule);
			Electricity =(int)( Electricity / car.MaxElectricQty.Value *100.00m);
			ElectricityWidth = Electricity * 2;
			CarStatusIcon = AGVCarStatus.CarStatusIcon[car.CarStatus];
            
        }


















	}
}
